<?php
namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Http\Requests\User\Author2Request;
use App\Http\Service\ThirdLoginService;
use App\Http\Requests\User\AddinfoRequest;
use App\Repositories\User\Author2Repository;
use Illuminate\Support\Facades\Cache;
use App\Traits\Third\TokenThird;
use App\Http\Requests\User\RegisterRequest;
use App\Http\Requests\User\LoginRequest;
use App\Http\Requests\User\EditInfoRequest;
use App\Repositories\System\MessageRepository;
use Crypt;

class Author2Controller extends Controller {
    use TokenThird;

    public function auth(Author2Request $request, Author2Repository $repository)
    {
        $code = $request->get("code");
        $state = $request->get("state");
        $authorService = ThirdLoginService::getInstance();
        $userInfo = $authorService->getThirdInfo($code, $state);
        $unionId = $userInfo['unionid']??'';
        $data = $this->resetArrayKeys($userInfo, $state);
        $res = $repository->getUserInfoByOpenId($userInfo['openid'], $state, $unionId, $data);
        $res = $res?(@$res->toArray()?:[]):[];
        if (!$res) return failed("注册或绑定时出现未知错误");
        Cache::put($state.":login:uid".$res['id'], $res);
        unset($res['password']);
        unset($res['is_delete']);
        unset($res['created_at']);
        unset($res['updated_at']);
        return success($res);
    }

    public function addInfo(AddinfoRequest $request, Author2Repository $repository)
    {
        $data = $request->all();
        $res = $repository->addUserInfo($data, $data['uid']);
        if($res) return success($res);
        return failed("未知错误,请重试");
    }

    public function uploadheadimg(AddinfoRequest $request, Author2Repository $repository)
    {
        $data = $request->all();
        $res = $repository->uploadHeadImg($data, $data['uid']);
        if($res) return success($res);
        return failed("未知错误,请重试");
    }

    public function register(RegisterRequest $request, Author2Repository $repository, MessageRepository $messages)
    {
        $number = $request->get("number");
        $field = FALSE;
        if(preg_match("/^1[3456789]\d{9}$/",$number)){
            $field = 'mobile';
        }
        if(preg_match("/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/",$number)){
            $field = 'email';
        }
        if($field){
            $data = $request->all();
            $res = $repository->register($data, $field);
            $res = $res?(@$res->toArray()?:[]):[];
            if($res) {
                $cdnName = config("system.cdn_name");
                $password = Crypt::decrypt($res['password']);
                $contents = "恭喜您已成功注册{$cdnName}会员!\n会员账户:{$res['number']}。\n会员密码:{$password}。\n可通过手机号,邮箱,账户进行登录操作\n希望我们的产品对您有所帮助!祝您使用愉快!";
                $messages->addMessages($res['id'], "恭喜您注册成功!", $contents);
                return success($res);
            }
            return failed("注册失败");
        }
        return failed("账号格式错误");
    }

    public function login(LoginRequest $request, Author2Repository $repository)
    {
        $number = $request->get("number");
        $field = FALSE;
        if(preg_match("/^1[3456789]\d{9}$/",$number)){
            $field = 'mobile';
        }
        if(preg_match("/^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/",$number)){
            $field = 'email';
        }
        if(preg_match("/^\d{8}$/", $number)){
            $field = 'number';
        }
        if($field){
            $data = $request->all();
            $res = $repository->login($data, $field);
            if($res) return success($res);
        }
        return failed("登录失败");
    }
}